"""
Case Type   : spq多机并行
Case Name   : gs_guc set 设置comm_sctp_port参数,取非法值合理报错
Create At   : 2023/12/15
Owner       : songjing20
Description :
    1.查看comm_sctp_port的值，查询pg_settings系统表
    2.设置为0、1、65535、数据库端口值
    3.设置为非法值
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.合理报错
    4.成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SpqPlugin(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.param = 'comm_sctp_port'
        self.values = ['0', '1', '65535', '7000', self.pri_node.db_port]
        self.err_values = ['-1', '65536', '38700.5', 'none', '*', '$', '/']

    def test_spq(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = f'-----step1:查看{self.param}的值，查询pg_settings系统表;expect:成功-----'
        self.log.info(text)
        sql = f"show {self.param};" \
              f"select * from pg_settings where name = '{self.param}';"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.para_value = sql_res.splitlines()[2].strip()
        self.assertEqual(str(int(self.pri_node.db_port) + 30),
                         self.para_value, f'执行失败:{text}')
        self.assertIn('Connections and Authentication / Connection Settings | '
                      'Sets the STCP port the server listens on. |            '
                      '| postmaster | integer | configuration file | 0       |'
                      ' 65535   |          | 7000', sql_res, f'执行失败:{text}')

        text = '---step2:设置为0、1、65535、数据库端口;expect:成功---'
        self.log.info(text)
        for value in self.values:
            self.log.info(f'-----设置为:{value}-----')
            res = self.pri_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, f"{self.param}={value}")
            self.log.info(res)
            self.assertTrue(res, f'执行失败:{text}')
            self.pri_sh.restart_db_cluster()
            res = self.common.show_param(self.param)
            self.assertEqual(res, str(value), f'执行失败:{text}')

        text = '-----step3:设置为非法值;expect:报错-----'
        self.log.info(text)
        for value in self.err_values:
            self.log.info(f'-----设置为:{value}-----')
            res = self.pri_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, f"{self.param}={value}")
            self.log.info(res)
            self.assertFalse(res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"{self.param}={int(self.pri_node.db_port) + 30}")
        self.log.info(res)
        restart_res = self.pri_sh.restart_db_cluster()
        self.log.info(restart_res)
        self.assertTrue(res, f'执行失败:{text}')
        self.assertTrue(restart_res, f'执行失败:{text}')
        self.log.info(f'----{os.path.basename(__file__)} end-----')
